{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "27b1c81b",
   "metadata": {},
   "source": [
    "# 基于 MindQuantum 0.7.0 实现半导体双量子点下，采用多基矢编码方法 MBE 实现最大割求解"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f56b08c5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "当前训练次数为：0, 损失函数值为：0.5682091\n",
      "MBE算法计算的最大割数为： 1.5\n",
      "\n",
      "当前训练次数为：1, 损失函数值为：-0.06772007\n",
      "MBE算法计算的最大割数为： 1.5\n",
      "\n",
      "当前训练次数为：2, 损失函数值为：-0.15884987\n",
      "MBE算法计算的最大割数为： 1.5\n",
      "\n",
      "当前训练次数为：3, 损失函数值为：0.21334288\n",
      "MBE算法计算的最大割数为： 1.5\n",
      "\n",
      "当前训练次数为：4, 损失函数值为：0.24638289\n",
      "MBE算法计算的最大割数为： 1.5\n",
      "\n",
      "当前训练次数为：5, 损失函数值为：-0.013833374\n",
      "MBE算法计算的最大割数为： 2.0\n",
      "\n",
      "当前训练次数为：6, 损失函数值为：-0.6490104\n",
      "MBE算法计算的最大割数为： 2.5\n",
      "\n",
      "当前训练次数为：7, 损失函数值为：-0.7773255\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：8, 损失函数值为：-0.9761011\n",
      "MBE算法计算的最大割数为： 2.0\n",
      "\n",
      "当前训练次数为：9, 损失函数值为：-0.83940554\n",
      "MBE算法计算的最大割数为： 2.5\n",
      "\n",
      "当前训练次数为：10, 损失函数值为：-0.78345203\n",
      "MBE算法计算的最大割数为： 2.5\n",
      "\n",
      "当前训练次数为：11, 损失函数值为：-0.81188357\n",
      "MBE算法计算的最大割数为： 2.0\n",
      "\n",
      "当前训练次数为：12, 损失函数值为：-0.7737508\n",
      "MBE算法计算的最大割数为： 2.0\n",
      "\n",
      "当前训练次数为：13, 损失函数值为：-0.87277997\n",
      "MBE算法计算的最大割数为： 2.5\n",
      "\n",
      "当前训练次数为：14, 损失函数值为：-0.8826096\n",
      "MBE算法计算的最大割数为： 2.5\n",
      "\n",
      "当前训练次数为：15, 损失函数值为：-0.9276724\n",
      "MBE算法计算的最大割数为： 2.5\n",
      "\n",
      "当前训练次数为：16, 损失函数值为：-1.0147531\n",
      "MBE算法计算的最大割数为： 2.0\n",
      "\n",
      "当前训练次数为：17, 损失函数值为：-1.0166626\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：18, 损失函数值为：-1.0720876\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：19, 损失函数值为：-1.0819068\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：20, 损失函数值为：-1.0347126\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：21, 损失函数值为：-1.0365372\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：22, 损失函数值为：-1.0841954\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：23, 损失函数值为：-1.0910609\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：24, 损失函数值为：-1.0672368\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：25, 损失函数值为：-1.067241\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：26, 损失函数值为：-1.0676425\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：27, 损失函数值为：-1.0912396\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：28, 损失函数值为：-1.1045973\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：29, 损失函数值为：-1.0703483\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：30, 损失函数值为：-1.0867789\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：31, 损失函数值为：-1.0987399\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：32, 损失函数值为：-1.0986038\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：33, 损失函数值为：-1.0976745\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：34, 损失函数值为：-1.0853763\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：35, 损失函数值为：-1.0999604\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：36, 损失函数值为：-1.1069648\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：37, 损失函数值为：-1.0996726\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：38, 损失函数值为：-1.1007428\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：39, 损失函数值为：-1.1018468\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：40, 损失函数值为：-1.1062355\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：41, 损失函数值为：-1.1045198\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：42, 损失函数值为：-1.1036168\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：43, 损失函数值为：-1.1087179\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：44, 损失函数值为：-1.105907\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：45, 损失函数值为：-1.105611\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：46, 损失函数值为：-1.1077073\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：47, 损失函数值为：-1.1093351\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：48, 损失函数值为：-1.1094689\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：49, 损失函数值为：-1.1061261\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：50, 损失函数值为：-1.1101192\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：51, 损失函数值为：-1.1097336\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：52, 损失函数值为：-1.1099844\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：53, 损失函数值为：-1.1090343\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：54, 损失函数值为：-1.1104448\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：55, 损失函数值为：-1.1105443\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：56, 损失函数值为：-1.1097441\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：57, 损失函数值为：-1.1111543\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：58, 损失函数值为：-1.1110244\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：59, 损失函数值为：-1.1106931\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：60, 损失函数值为：-1.1102895\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：61, 损失函数值为：-1.111848\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：62, 损失函数值为：-1.1114062\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：63, 损失函数值为：-1.1109219\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：64, 损失函数值为：-1.1111572\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：65, 损失函数值为：-1.1118133\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：66, 损失函数值为：-1.1115863\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：67, 损失函数值为：-1.1113104\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：68, 损失函数值为：-1.1117563\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：69, 损失函数值为：-1.1116626\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：70, 损失函数值为：-1.1116868\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：71, 损失函数值为：-1.111768\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：72, 损失函数值为：-1.1119218\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：73, 损失函数值为：-1.1116413\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：74, 损失函数值为：-1.1118965\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：75, 损失函数值为：-1.1119432\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：76, 损失函数值为：-1.1119287\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：77, 损失函数值为：-1.1118119\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：78, 损失函数值为：-1.1120235\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：79, 损失函数值为：-1.1119456\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：80, 损失函数值为：-1.1119843\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：81, 损失函数值为：-1.1119924\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：82, 损失函数值为：-1.1120195\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：83, 损失函数值为：-1.1119587\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：84, 损失函数值为：-1.1120965\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：85, 损失函数值为：-1.1120327\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：86, 损失函数值为：-1.112015\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：87, 损失函数值为：-1.112046\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：88, 损失函数值为：-1.112115\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：89, 损失函数值为：-1.112037\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：90, 损失函数值为：-1.1120632\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：91, 损失函数值为：-1.112099\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：92, 损失函数值为：-1.1120797\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：93, 损失函数值为：-1.1120849\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：94, 损失函数值为：-1.1120989\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：95, 损失函数值为：-1.1120924\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：96, 损失函数值为：-1.1120914\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：97, 损失函数值为：-1.112119\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：98, 损失函数值为：-1.1120958\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "当前训练次数为：99, 损失函数值为：-1.1121033\n",
      "MBE算法计算的最大割数为： 3.0\n",
      "\n",
      "loss_list:\n",
      " [Tensor(shape=[], dtype=Float32, value= 0.568209), Tensor(shape=[], dtype=Float32, value= -0.0677201), Tensor(shape=[], dtype=Float32, value= -0.15885), Tensor(shape=[], dtype=Float32, value= 0.213343), Tensor(shape=[], dtype=Float32, value= 0.246383), Tensor(shape=[], dtype=Float32, value= -0.0138334), Tensor(shape=[], dtype=Float32, value= -0.64901), Tensor(shape=[], dtype=Float32, value= -0.777326), Tensor(shape=[], dtype=Float32, value= -0.976101), Tensor(shape=[], dtype=Float32, value= -0.839406), Tensor(shape=[], dtype=Float32, value= -0.783452), Tensor(shape=[], dtype=Float32, value= -0.811884), Tensor(shape=[], dtype=Float32, value= -0.773751), Tensor(shape=[], dtype=Float32, value= -0.87278), Tensor(shape=[], dtype=Float32, value= -0.88261), Tensor(shape=[], dtype=Float32, value= -0.927672), Tensor(shape=[], dtype=Float32, value= -1.01475), Tensor(shape=[], dtype=Float32, value= -1.01666), Tensor(shape=[], dtype=Float32, value= -1.07209), Tensor(shape=[], dtype=Float32, value= -1.08191), Tensor(shape=[], dtype=Float32, value= -1.03471), Tensor(shape=[], dtype=Float32, value= -1.03654), Tensor(shape=[], dtype=Float32, value= -1.0842), Tensor(shape=[], dtype=Float32, value= -1.09106), Tensor(shape=[], dtype=Float32, value= -1.06724), Tensor(shape=[], dtype=Float32, value= -1.06724), Tensor(shape=[], dtype=Float32, value= -1.06764), Tensor(shape=[], dtype=Float32, value= -1.09124), Tensor(shape=[], dtype=Float32, value= -1.1046), Tensor(shape=[], dtype=Float32, value= -1.07035), Tensor(shape=[], dtype=Float32, value= -1.08678), Tensor(shape=[], dtype=Float32, value= -1.09874), Tensor(shape=[], dtype=Float32, value= -1.0986), Tensor(shape=[], dtype=Float32, value= -1.09767), Tensor(shape=[], dtype=Float32, value= -1.08538), Tensor(shape=[], dtype=Float32, value= -1.09996), Tensor(shape=[], dtype=Float32, value= -1.10696), Tensor(shape=[], dtype=Float32, value= -1.09967), Tensor(shape=[], dtype=Float32, value= -1.10074), Tensor(shape=[], dtype=Float32, value= -1.10185), Tensor(shape=[], dtype=Float32, value= -1.10624), Tensor(shape=[], dtype=Float32, value= -1.10452), Tensor(shape=[], dtype=Float32, value= -1.10362), Tensor(shape=[], dtype=Float32, value= -1.10872), Tensor(shape=[], dtype=Float32, value= -1.10591), Tensor(shape=[], dtype=Float32, value= -1.10561), Tensor(shape=[], dtype=Float32, value= -1.10771), Tensor(shape=[], dtype=Float32, value= -1.10934), Tensor(shape=[], dtype=Float32, value= -1.10947), Tensor(shape=[], dtype=Float32, value= -1.10613), Tensor(shape=[], dtype=Float32, value= -1.11012), Tensor(shape=[], dtype=Float32, value= -1.10973), Tensor(shape=[], dtype=Float32, value= -1.10998), Tensor(shape=[], dtype=Float32, value= -1.10903), Tensor(shape=[], dtype=Float32, value= -1.11044), Tensor(shape=[], dtype=Float32, value= -1.11054), Tensor(shape=[], dtype=Float32, value= -1.10974), Tensor(shape=[], dtype=Float32, value= -1.11115), Tensor(shape=[], dtype=Float32, value= -1.11102), Tensor(shape=[], dtype=Float32, value= -1.11069), Tensor(shape=[], dtype=Float32, value= -1.11029), Tensor(shape=[], dtype=Float32, value= -1.11185), Tensor(shape=[], dtype=Float32, value= -1.11141), Tensor(shape=[], dtype=Float32, value= -1.11092), Tensor(shape=[], dtype=Float32, value= -1.11116), Tensor(shape=[], dtype=Float32, value= -1.11181), Tensor(shape=[], dtype=Float32, value= -1.11159), Tensor(shape=[], dtype=Float32, value= -1.11131), Tensor(shape=[], dtype=Float32, value= -1.11176), Tensor(shape=[], dtype=Float32, value= -1.11166), Tensor(shape=[], dtype=Float32, value= -1.11169), Tensor(shape=[], dtype=Float32, value= -1.11177), Tensor(shape=[], dtype=Float32, value= -1.11192), Tensor(shape=[], dtype=Float32, value= -1.11164), Tensor(shape=[], dtype=Float32, value= -1.1119), Tensor(shape=[], dtype=Float32, value= -1.11194), Tensor(shape=[], dtype=Float32, value= -1.11193), Tensor(shape=[], dtype=Float32, value= -1.11181), Tensor(shape=[], dtype=Float32, value= -1.11202), Tensor(shape=[], dtype=Float32, value= -1.11195), Tensor(shape=[], dtype=Float32, value= -1.11198), Tensor(shape=[], dtype=Float32, value= -1.11199), Tensor(shape=[], dtype=Float32, value= -1.11202), Tensor(shape=[], dtype=Float32, value= -1.11196), Tensor(shape=[], dtype=Float32, value= -1.1121), Tensor(shape=[], dtype=Float32, value= -1.11203), Tensor(shape=[], dtype=Float32, value= -1.11202), Tensor(shape=[], dtype=Float32, value= -1.11205), Tensor(shape=[], dtype=Float32, value= -1.11212), Tensor(shape=[], dtype=Float32, value= -1.11204), Tensor(shape=[], dtype=Float32, value= -1.11206), Tensor(shape=[], dtype=Float32, value= -1.1121), Tensor(shape=[], dtype=Float32, value= -1.11208), Tensor(shape=[], dtype=Float32, value= -1.11208), Tensor(shape=[], dtype=Float32, value= -1.1121), Tensor(shape=[], dtype=Float32, value= -1.11209), Tensor(shape=[], dtype=Float32, value= -1.11209), Tensor(shape=[], dtype=Float32, value= -1.11212), Tensor(shape=[], dtype=Float32, value= -1.1121), Tensor(shape=[], dtype=Float32, value= -1.1121)]\n",
      "\n",
      "res_list:\n",
      " [Tensor(shape=[], dtype=Float32, value= 1.5), Tensor(shape=[], dtype=Float32, value= 1.5), Tensor(shape=[], dtype=Float32, value= 1.5), Tensor(shape=[], dtype=Float32, value= 1.5), Tensor(shape=[], dtype=Float32, value= 1.5), Tensor(shape=[], dtype=Float32, value= 2), Tensor(shape=[], dtype=Float32, value= 2.5), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 2), Tensor(shape=[], dtype=Float32, value= 2.5), Tensor(shape=[], dtype=Float32, value= 2.5), Tensor(shape=[], dtype=Float32, value= 2), Tensor(shape=[], dtype=Float32, value= 2), Tensor(shape=[], dtype=Float32, value= 2.5), Tensor(shape=[], dtype=Float32, value= 2.5), Tensor(shape=[], dtype=Float32, value= 2.5), Tensor(shape=[], dtype=Float32, value= 2), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3), Tensor(shape=[], dtype=Float32, value= 3)]\n"
     ]
    }
   ],
   "source": [
    "import copy\n",
    "import numpy as np\n",
    "import mindspore as ms\n",
    "from numpy import kron\n",
    "from mindquantum import *\n",
    "from scipy.linalg import expm\n",
    "from mindspore.ops import operations\n",
    "from mindspore import nn, ops, Tensor, context\n",
    "from mindspore.common.parameter import Parameter\n",
    "from mindspore.common.initializer import initializer  \n",
    "from mindspore.nn import Adam, TrainOneStepCell, LossBase\n",
    "ms.context.set_context(mode=ms.context.PYNATIVE_MODE, device_target=\"CPU\")\n",
    "ms.set_seed(1)\n",
    "np.random.seed(1)\n",
    "\n",
    "s_x = X.matrix()\n",
    "s_z = Z.matrix()\n",
    "one = I.matrix()\n",
    "dt = np.pi/2\n",
    "ddt = np.pi/10\n",
    "\n",
    "def _matrix_(coeff):\n",
    "    return expm(-1j*(coeff*s_z+s_x)*dt)\n",
    "\n",
    "def _diff_matrix_(coeff):\n",
    "    return -1j*_matrix_(coeff)@(s_z*dt)\n",
    "\n",
    "def _matrix_0(coeff):\n",
    "    return expm(-1j*(coeff*s_z+s_x)*ddt)\n",
    "\n",
    "def _diff_matrix_0(coeff):\n",
    "    return -1j*_matrix_0(coeff)@(s_z*ddt)\n",
    "\n",
    "def _matrix_c_0(coeff):\n",
    "    return expm(-1j*(coeff*kron(s_z, one) + kron(one, s_z) + kron(s_x, one) + kron(one, s_x) + coeff*kron(s_z-one, s_z-one))*5*ddt)\n",
    "\n",
    "def _diff_matrix_c_0(coeff):\n",
    "    return -1j*_matrix_c_0(coeff)@((kron(s_z, one) + kron(s_z-one, s_z-one)) * 5*ddt)\n",
    "\n",
    "def _matrix_c_1(coeff):\n",
    "    return expm(-1j*(kron(s_z, one) + coeff*kron(one, s_z) + kron(s_x, one) + kron(one, s_x) + coeff*kron(s_z-one, s_z-one))*5*ddt)\n",
    "\n",
    "def _diff_matrix_c_1(coeff):\n",
    "    return -1j*_matrix_c_1(coeff)@((kron(one, s_z) + kron(s_z-one, s_z-one)) *  5*ddt)\n",
    "\n",
    "gate = gene_univ_parameterized_gate('gete', _matrix_, _diff_matrix_) # dt=pi/2\n",
    "gate_0 = gene_univ_parameterized_gate('gete_0', _matrix_0, _diff_matrix_0) # ddt=pi/10\n",
    "gate_c_0 = gene_univ_parameterized_gate('gete_c_0', _matrix_c_0, _diff_matrix_c_0) # ddt=pi/10\n",
    "gate_c_1 = gene_univ_parameterized_gate('gete_c_1', _matrix_c_1, _diff_matrix_c_1) # ddt=pi/10\n",
    "\n",
    "cz_params = np.array( [1.5472503,  1.4179231,  1.540713,   1.9724044,  1.9253408,  1.3879265,\n",
    "                     0.8130467,  0.76446086, 1.2703444,  1.8553745,  1.0291328,  1.2492974,\n",
    "                     0.7880994,  0.3026381,  0.31203356, 0.30834132, 0.9533752,  1.3802187,\n",
    "                     1.270656,   0.5646567,  0.94619316, 0.97377133, 1.9658349,  0.83277696,\n",
    "                     1.0190777,  0.90001523, 0.26008993, 0.16526282, 0.22249524, 1.1596956,\n",
    "                     1.5285202,  0.4919534,  0.01645389, 0.02608137, 0.6504683,  0.31325826,\n",
    "                     0.4486266,  0.8677286,  1.3571227,  1.4995408,  1.1248059,  0.5996333,\n",
    "                     0.32797617, 0.54987127])\n",
    "# params_name = ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '0', '1', '2', '3', '010', '011', '012', '013', '014', '015', '016', '017', '018', '019', '110', '111', '112', '113', '114', '115', '116', '117', '118', '119']\n",
    "\n",
    "def cz_circ():\n",
    "    circ_ = Circuit()\n",
    "    circ_ += BarrierGate()\n",
    "    circ_ += Circuit([gate_0(param).on(0) for param in cz_params[:10]])\n",
    "    circ_ += Circuit([gate_0(0).on(1) for i in range(10)])\n",
    "    circ_ += BarrierGate()\n",
    "    circ_ += Circuit([gate_0(0).on(0) for i in range(10)])\n",
    "    circ_ += Circuit([gate_0(param).on(1) for param in cz_params[10:20]])\n",
    "    circ_ += Circuit([gate_c_0(cz_params[20]).on([1,0]), gate_c_0(cz_params[21]).on([1,0])])\n",
    "    circ_ += Circuit([gate_c_1(cz_params[22]).on([1,0]), gate_c_1(cz_params[23]).on([1,0])])\n",
    "    circ_ += Circuit([gate_0(param).on(0) for param in cz_params[24:34]])\n",
    "    circ_ += Circuit([gate_0(0).on(1) for i in range(10,20)])\n",
    "    circ_ += BarrierGate()\n",
    "    circ_ += Circuit([gate_0(0).on(0) for i in range(10)])\n",
    "    circ_ += Circuit([gate_0(param).on(1) for param in cz_params[34:]])\n",
    "    circ_ += BarrierGate()\n",
    "    return circ_\n",
    "\n",
    "def arb_circ(prefix='0'):\n",
    "    circ_ = Circuit()\n",
    "    circ_ += BarrierGate()\n",
    "    circ_ += Circuit([gate(f'arb0{i}').on(0) for i in range(12)])\n",
    "    circ_ += Circuit([gate(0).on(1) for i in range(12)])\n",
    "    circ_ += BarrierGate()\n",
    "    circ_ += Circuit([gate(0).on(0) for i in range(12)])\n",
    "    circ_ += Circuit([gate(f'arb1{i}').on(1) for i in range(12)])\n",
    "    circ_ += BarrierGate()\n",
    "    circ_ = add_prefix(circ_, prefix)\n",
    "    return circ_\n",
    "\n",
    "def qlayer(prefix='0'):\n",
    "    circ_ = Circuit()\n",
    "    circ_ += arb_circ(prefix)\n",
    "    circ_ += cz_circ()\n",
    "    return circ_\n",
    "\n",
    "class my_simulator: \n",
    "    def get_expectation_with_grad(self, hams, circ): # 输入为 hams, circ_right, simulator_left 等，这里就只用 hams 代替。\n",
    "        sim = Simulator('projectq', 2) \n",
    "        \n",
    "        def grad_ops(inputs): # 输入为各量子门的参数\n",
    "            h = 1e-4\n",
    "            grad = []\n",
    "            sim.reset()\n",
    "            sim.apply_circuit(circ, inputs)\n",
    "            exceptation = [sim.get_expectation(ham).real for ham in hams]\n",
    "            for i in range(inputs.size):\n",
    "                params_p, params_n = copy.deepcopy(inputs), copy.deepcopy(inputs)\n",
    "                params_p[i] += h\n",
    "                params_n[i] -= h\n",
    "                sim.reset()\n",
    "                sim.apply_circuit(circ, params_p)\n",
    "                g_p = [sim.get_expectation(ham) for ham in hams]\n",
    "                sim.reset()\n",
    "                sim.apply_circuit(circ, params_n)\n",
    "                g_n = [sim.get_expectation(ham) for ham in hams]\n",
    "                grad.append([(p.real - n.real)/(2*h) for p, n in zip(g_p, g_n)])\n",
    "                sim.reset()\n",
    "            return exceptation, np.array(grad).T # 运行结果为期望值和梯度\n",
    "        return grad_ops # MindQuantum 中，这里返回的是一个封装了 hams, grad_ops 等的一个封装器，这里简化为单独的 grad_ops\n",
    "\n",
    "class ansatz_only_ops(nn.Cell):\n",
    "    def __init__(self, expectation_with_grad):\n",
    "        super().__init__()\n",
    "        self.expectation_with_grad = expectation_with_grad\n",
    "        self.shape_ops = operations.Shape()\n",
    "        self.g = None \n",
    "\n",
    "    def construct(self, arg):\n",
    "        fval, g_ans = self.expectation_with_grad(arg.asnumpy())\n",
    "        self.g = np.real(g_ans)\n",
    "        return ms.Tensor(np.real(fval), dtype=ms.float32)\n",
    "\n",
    "    def bprop(self, arg, out, dout): \n",
    "        dout = dout.asnumpy()\n",
    "        grad = dout @ self.g\n",
    "        return ms.Tensor(grad, dtype=ms.float32)\n",
    "\n",
    "class ansatz_only_layer(nn.Cell):\n",
    "    def __init__(self, expectation_with_grad, weight='ones'):\n",
    "        super().__init__()\n",
    "        self.evolution = ansatz_only_ops(expectation_with_grad)\n",
    "        weight_size = len(circ.params_name)\n",
    "        self.weight = Parameter(initializer(weight, weight_size, dtype=ms.float32), name='ansatz_weight')\n",
    "        self.abs = ops.Abs()\n",
    "\n",
    "    def construct(self):\n",
    "        return self.evolution(self.abs(self.weight))\n",
    "\n",
    "class MyLoss(LossBase):\n",
    "    def __init__(self, reduction='mean'):\n",
    "        super(MyLoss, self).__init__(reduction)\n",
    "        self.tanh = ops.Tanh()\n",
    "\n",
    "    def construct(self, logits):\n",
    "        out = self.tanh(logits)\n",
    "        out = out[0] * out[2] + out[1] * out[3] + out[0] * out[1] \n",
    "        return self.get_loss(out)\n",
    "\n",
    "class MyWithLossCell(nn.Cell):\n",
    "    def __init__(self, backbone, loss_fn):\n",
    "       super(MyWithLossCell, self).__init__(auto_prefix=False)\n",
    "       self._backbone = backbone\n",
    "       self._loss_fn = loss_fn\n",
    "\n",
    "    def construct(self):\n",
    "       out = self._backbone()\n",
    "       return self._loss_fn(out)\n",
    "\n",
    "    @property\n",
    "    def backbone_network(self):\n",
    "       return self._backbone\n",
    "\n",
    "\n",
    "circ = Circuit()\n",
    "for i in range(2):\n",
    "    circ += qlayer(prefix=f'{i}')\n",
    "\n",
    "hams = [Hamiltonian(QubitOperator('Z0')), Hamiltonian(QubitOperator('X0')), \n",
    "        Hamiltonian(QubitOperator('Z1')), Hamiltonian(QubitOperator('X1'))]\n",
    "\n",
    "my_sim = my_simulator()\n",
    "grad_ops = my_sim.get_expectation_with_grad(hams, circ)\n",
    "\n",
    "qnet = ansatz_only_layer(grad_ops)\n",
    "loss = MyLoss()\n",
    "net_with_criterion = MyWithLossCell(qnet, loss)\n",
    "opti = Adam(qnet.trainable_params(), learning_rate=0.1) \n",
    "net = TrainOneStepCell(net_with_criterion, opti)\n",
    "\n",
    "round = ops.Round()\n",
    "loss_list = []\n",
    "res_list = []\n",
    "for i in range(100):\n",
    "    loss = net()\n",
    "    loss_list.append(loss)\n",
    "    print(f'\\n当前训练次数为：{i}, 损失函数值为：{loss}')\n",
    "    out = qnet()\n",
    "    res = (1 - round(out[0]) * round(out[2])) / 2 + (1 - round(out[1]) * round(out[3])) / 2 + (1 - round(out[0]) * round(out[1])) / 2\n",
    "    print('MBE算法计算的最大割数为：', res) #int(res + 0.5))\n",
    "    res_list.append(res) #int(res + 0.5))\n",
    "    \n",
    "print('\\nloss_list:\\n', loss_list)\n",
    "print('\\nres_list:\\n', res_list)\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "46371af6",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "loss_array\n",
      " [ 0.56820911 -0.06772007 -0.15884987  0.21334288  0.24638289 -0.01383337\n",
      " -0.64901042 -0.77732551 -0.9761011  -0.83940554 -0.78345203 -0.81188357\n",
      " -0.77375078 -0.87277997 -0.88260961 -0.92767239 -1.0147531  -1.0166626\n",
      " -1.07208765 -1.0819068  -1.03471255 -1.03653717 -1.08419538 -1.09106088\n",
      " -1.06723678 -1.06724095 -1.06764245 -1.09123957 -1.10459733 -1.07034826\n",
      " -1.08677888 -1.09873986 -1.09860384 -1.09767449 -1.08537626 -1.09996045\n",
      " -1.10696483 -1.09967256 -1.10074282 -1.10184681 -1.1062355  -1.10451984\n",
      " -1.10361683 -1.10871792 -1.10590696 -1.10561097 -1.10770726 -1.10933506\n",
      " -1.10946894 -1.10612607 -1.11011922 -1.10973358 -1.1099844  -1.1090343\n",
      " -1.11044478 -1.11054432 -1.10974407 -1.11115432 -1.11102438 -1.1106931\n",
      " -1.11028945 -1.111848   -1.11140621 -1.11092186 -1.11115718 -1.11181331\n",
      " -1.11158633 -1.11131036 -1.11175632 -1.11166263 -1.11168683 -1.11176801\n",
      " -1.11192179 -1.11164129 -1.11189651 -1.11194324 -1.1119287  -1.11181188\n",
      " -1.11202347 -1.11194563 -1.11198425 -1.11199236 -1.11201954 -1.11195874\n",
      " -1.11209655 -1.11203265 -1.11201501 -1.112046   -1.11211503 -1.11203694\n",
      " -1.11206317 -1.11209905 -1.11207974 -1.11208487 -1.11209893 -1.11209238\n",
      " -1.11209142 -1.11211896 -1.11209583 -1.11210334]\n",
      "\n",
      "res_array\n",
      " [1.5 1.5 1.5 1.5 1.5 2.  2.5 3.  2.  2.5 2.5 2.  2.  2.5 2.5 2.5 2.  3.\n",
      " 3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.\n",
      " 3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.\n",
      " 3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.\n",
      " 3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.\n",
      " 3.  3.  3.  3.  3.  3.  3.  3.  3.  3. ]\n"
     ]
    }
   ],
   "source": [
    "loss_array = np.array([])\n",
    "for i in loss_list:\n",
    "    loss_array = np.append(loss_array, i.asnumpy())\n",
    "    \n",
    "res_array = np.array([])\n",
    "for i in res_list:\n",
    "    res_array = np.append(res_array, i.asnumpy())\n",
    "    \n",
    "print('loss_array\\n', loss_array)\n",
    "print('\\nres_array\\n', res_array)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "006e14d7",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "loss_array\n",
      " [ 0.56820911 -0.06772007 -0.15884987  0.21334288  0.24638289 -0.01383337\n",
      " -0.64901042 -0.77732551 -0.9761011  -0.83940554 -0.78345203 -0.81188357\n",
      " -0.77375078 -0.87277997 -0.88260961 -0.92767239 -1.0147531  -1.0166626\n",
      " -1.07208765 -1.0819068  -1.03471255 -1.03653717 -1.08419538 -1.09106088\n",
      " -1.06723678 -1.06724095 -1.06764245 -1.09123957 -1.10459733 -1.07034826\n",
      " -1.08677888 -1.09873986 -1.09860384 -1.09767449 -1.08537626 -1.09996045\n",
      " -1.10696483 -1.09967256 -1.10074282 -1.10184681 -1.1062355  -1.10451984\n",
      " -1.10361683 -1.10871792 -1.10590696 -1.10561097 -1.10770726 -1.10933506\n",
      " -1.10946894 -1.10612607 -1.11011922 -1.10973358 -1.1099844  -1.1090343\n",
      " -1.11044478 -1.11054432 -1.10974407 -1.11115432 -1.11102438 -1.1106931\n",
      " -1.11028945 -1.111848   -1.11140621 -1.11092186 -1.11115718 -1.11181331\n",
      " -1.11158633 -1.11131036 -1.11175632 -1.11166263 -1.11168683 -1.11176801\n",
      " -1.11192179 -1.11164129 -1.11189651 -1.11194324 -1.1119287  -1.11181188\n",
      " -1.11202347 -1.11194563 -1.11198425 -1.11199236 -1.11201954 -1.11195874\n",
      " -1.11209655 -1.11203265 -1.11201501 -1.112046   -1.11211503 -1.11203694\n",
      " -1.11206317 -1.11209905 -1.11207974 -1.11208487 -1.11209893 -1.11209238\n",
      " -1.11209142 -1.11211896 -1.11209583 -1.11210334]\n",
      "\n",
      "res_array\n",
      " [1.5 1.5 1.5 1.5 1.5 2.  2.5 3.  2.  2.5 2.5 2.  2.  2.5 2.5 2.5 2.  3.\n",
      " 3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.\n",
      " 3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.\n",
      " 3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.\n",
      " 3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.\n",
      " 3.  3.  3.  3.  3.  3.  3.  3.  3.  3. ]\n"
     ]
    }
   ],
   "source": [
    "loss_array = np.array([])\n",
    "for i in range(100):\n",
    "    loss_array = np.append(loss_array, loss_list[i].asnumpy())\n",
    "    \n",
    "res_array = np.array([])\n",
    "for i in range(100):\n",
    "    res_array = np.append(res_array, res_list[i].asnumpy())\n",
    "    \n",
    "print('loss_array\\n', loss_array)\n",
    "print('\\nres_array\\n', res_array)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5d9f69fe",
   "metadata": {},
   "outputs": [],
   "source": [
    "0.56820911 -0.06772007 -0.15884987  0.21334288  0.24638289 -0.01383337 -0.64901042 -0.77732551 -0.9761011  -0.83940554 -0.78345203 -0.81188357 -0.77375078 -0.87277997 -0.88260961 -0.92767239 -1.0147531  -1.0166626 -1.07208765 -1.0819068  -1.03471255 -1.03653717 -1.08419538 -1.09106088 -1.06723678 -1.06724095 -1.06764245 -1.09123957 -1.10459733 -1.07034826 -1.08677888 -1.09873986 -1.09860384 -1.09767449 -1.08537626 -1.09996045 -1.10696483 -1.09967256 -1.10074282 -1.10184681 -1.1062355  -1.10451984 -1.10361683 -1.10871792 -1.10590696 -1.10561097 -1.10770726 -1.10933506 -1.10946894 -1.10612607 -1.11011922 -1.10973358 -1.1099844  -1.1090343 -1.11044478 -1.11054432 -1.10974407 -1.11115432 -1.11102438 -1.1106931 -1.11028945 -1.111848   -1.11140621 -1.11092186 -1.11115718 -1.11181331 -1.11158633 -1.11131036 -1.11175632 -1.11166263 -1.11168683 -1.11176801 -1.11192179 -1.11164129 -1.11189651 -1.11194324 -1.1119287  -1.11181188 -1.11202347 -1.11194563 -1.11198425 -1.11199236 -1.11201954 -1.11195874 -1.11209655 -1.11203265 -1.11201501 -1.112046   -1.11211503 -1.11203694 -1.11206317 -1.11209905 -1.11207974 -1.11208487 -1.11209893 -1.11209238 -1.11209142 -1.11211896 -1.11209583 -1.11210334"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "155b6c1a",
   "metadata": {},
   "outputs": [],
   "source": [
    "1.5 1.5 1.5 1.5 1.5 2.  2.5 3.  2.  2.5 2.5 2.  2.  2.5 2.5 2.5 2.  3. 3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3. 3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3. 3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3. 3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3. 3.  3.  3.  3.  3.  3.  3.  3.  3.  3."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
